# -*- coding: utf-8 -*-

'''
Created on Apr 27, 2011

@author: Bartosz Alchimowicz

Generowanie wyrażenie regularnego dla non-negative integers (0, 1, 2, 3...).

Supported format:

format jak dla interger
'''

class Interval:
	def __init__(self, lower, upper):
		self.lower = lower
		self.upper = upper

class Regexp:	
	def __init__(self, lower, upper):
		self.lower = lower
		self.upper = upper
		
	def __repr__(self):
		if self.upper == self.lower:
			return str(self.lower)
		else:
			return '[' + self.lower + '-' + self.upper + ']'

def sum_regexp(regexp_list):
	result = ''
	for regexp in regexp_list:
		result += regexp + '|'
	return result[:-1]

def list_to_string(list):
	result = ''
	for item in list:
		result += str(item)
	return result

def merge_chunks(regexp1_chunks, regexp2_chunks):
	
	result = list() 
	
	for i in xrange(0, len(regexp1_chunks)):
		merged = Regexp('', '')
		if regexp1_chunks[i].lower < regexp2_chunks[i].lower:
			merged.lower = regexp2_chunks[i].lower
		else:
			merged.lower = regexp1_chunks[i].lower
			
		if regexp1_chunks[i].upper < regexp2_chunks[i].upper:
			merged.upper = regexp1_chunks[i].upper
		else:
			merged.upper = regexp2_chunks[i].upper
			
		result.append(merged)
			
	return result

def generate_regexp_for_interval(result, interval, pointer = -1):
	
	regexp1_chunks = list()
	regexp2_chunks = list()
	
	lower_str = str(interval.lower)
	upper_str = str(interval.upper)
	next_value = ''
	prev_value = ''
	
	regexp = ''
	
	regexp = lower_str[:pointer] + '[' + lower_str[len(lower_str) + pointer] + '-9]'
	if lower_str[:pointer] != '':
		regexp1_chunks.append(Regexp(lower_str[:pointer], lower_str[:pointer]))
	regexp1_chunks.append(Regexp(lower_str[len(lower_str) + pointer], '9'))
	
	for i in xrange(1, -pointer):
		regexp += '[0-9]'
		regexp1_chunks.append(Regexp('0', '9'))
		
	result.append(regexp)
	
	next_value = lower_str[:pointer]
	for i in xrange(0, -pointer):
		next_value += '9'
			
	regexp = upper_str[:pointer] + '[0-' + upper_str[len(upper_str) + pointer] + ']'
	if upper_str[:pointer] != '':
		regexp2_chunks.append(Regexp(upper_str[:pointer], upper_str[:pointer]))
	regexp2_chunks.append(Regexp('0', upper_str[len(upper_str) + pointer]))
	
	for i in xrange(1, -pointer):
		regexp += '[0-9]'
		regexp2_chunks.append(Regexp('0', '9'))
		
	result.append(regexp)
	
	prev_value = upper_str[:pointer]
	for i in xrange(0, -pointer):
		prev_value += '0'
		
	if int(next_value) <= int(prev_value) - 1:
		pointer -= 1
		generate_regexp_for_interval(result, Interval(int(next_value) + 1, int(prev_value) - 1), pointer)
	else:
		merged = merge_chunks(regexp1_chunks, regexp2_chunks)
		result.pop()
		result.pop()
		result.append(list_to_string(merged))

def default(format, min, max):
	interval = Interval(min, max)
	result = list()
	generate_regexp_for_interval(result, interval)
	return sum_regexp(result)

def no_leading_zeros(format, min, max):
	interval = Interval(min, max)
	result = list()
	generate_regexp_for_interval(result, interval)
	return sum_regexp(result)

def run(format, min, max):
	if min > max:
		raise Exception("Min value cannot be greater than max!")
	
	numbers = format[1:-1]
	if len(numbers) == 2:
		num_of_digits = int(numbers[-1:])
		if num_of_digits != len(str(max)):
			raise Exception("Length mismatch!") 
	
	result = default(format, min, max)

	return result
